package com.base.controller.utils;

import com.base.comm.CommUrls;
import com.base.po.voip.NewsJH;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class NewsUtil {
//	{
//		"pk": "d6e4f544441eb0da2686bb7210781523",
//		"title": "七大机构基金评级结果出炉 博时21只基金获五星评级",
//		"date": "2018-08-22 14:14",
//		"date2": "2018-08-22 14:14:53",
//		"category": "caijing",
//		"author_name": "中国经济网",
//		"url": "https://mini.eastday.com/mobile/180822141453713.html",
//		"thumbnail_pic_s": "https://08imgmini.eastday.com/mobile/20180822/20180822141453_17b0a2d7cbef2103a2e0216342173404_3_mwpm_03200403.jpg",
//		"thumbnail_pic_s2": "https://08imgmini.eastday.com/mobile/20180822/20180822141453_17b0a2d7cbef2103a2e0216342173404_2_mwpm_03200403.jpg",
//		"thumbnail_pic_s3": "https://08imgmini.eastday.com/mobile/20180822/20180822141453_17b0a2d7cbef2103a2e0216342173404_1_mwpm_03200403.jpg",
//		"thumbnail_pic_w": 320,
//		"thumbnail_pic_h": 240
//	}

	//api接口

	public static JsonNode newsList() {
		String newsJsom = newsJson();
		ObjectMapper mapper = new ObjectMapper();
		JsonNode rootNode;
		try {
			rootNode = mapper.readTree(newsJsom);// 读取Json
			JsonNode data = rootNode.path("data");// rootNode.path("xx")返回的还是一个JsonNode对象，调用该JsonNode的相应方法，得到键对应的值
			// 遍历 info 内的 array
			if (data.isArray()) {
				return data;
			}
		} catch (JsonProcessingException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		return null;
	}


	// 新闻api接口读取数据
	public static String newsJson() {
		URL url = null;
//		HttpURLConnection httpConn = null;
		BufferedReader in = null;

		StringBuffer sb = new StringBuffer();
		try {
			url = new URL("http://newswifiapi.dfshurufa.com/newspool/topnews");
			in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
			String str = null;
			while ((str = in.readLine()) != null) {
				sb.append(str);
			}
		} catch (Exception ex) {
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
			}
		}
		String data = sb.toString();

		return data;
	}

	// 新闻api接口读取数据
	public static List<NewsJH> jhNewsList() {
		URL url = null;
//		HttpURLConnection httpConn = null;
		BufferedReader in = null;

		List<NewsJH> newsJHList = new ArrayList<>();

		StringBuffer sb = new StringBuffer();
		try {
			url = new URL("http://v.juhe.cn/toutiao/index?type=top&key=" + CommUrls.JHNEWSKEY);
			in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
			String str = null;
			while ((str = in.readLine()) != null) {
				sb.append(str);
			}
		} catch (Exception ex) {
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
			}
		}
		//如传过来的值为string类型则转成json类型
		JSONObject json = JSONObject.fromObject(sb.toString());
		//获取key值为result的json数据（就是json数据中嵌套一个json数据）
		JSONObject result=json.getJSONObject("result");
		//获取key值为data的json数组
		JSONArray data = result.getJSONArray("data");

		NewsJH newsJH = null;
		for(int i = 0;i<data.size();i++) {
			JSONObject jsons=data.getJSONObject(i);

			 if (!jsons.has("thumbnail_pic_s02")){
				  jsons.put("thumbnail_pic_s02", "");
			 }
			 if (!jsons.has("thumbnail_pic_s03")){
				  jsons.put("thumbnail_pic_s03", "");
			 }

			newsJH = new NewsJH(jsons.getString("uniquekey"),jsons.getString("title"),
					 jsons.getString("date"),
					 jsons.getString("category"),jsons.getString("author_name"),
					 jsons.getString("url"), jsons.getString("thumbnail_pic_s"),
					 jsons.getString("thumbnail_pic_s02"), jsons.getString("thumbnail_pic_s03"));
			newsJHList.add(newsJH);
		}

		return newsJHList;
	}
}
